//
//  ClientAddController.m
//  StarOrangeStaff
//
//  Created by lshenrong on 2020/6/23.
//  Copyright © 2020 lshenrong. All rights reserved.
//

#import "ClientAddController.h"
#import "InputTempletTwelveTableViewCell.h"
#import "FSActionSheet.h"
#import "ClientAddAPI.h"


@interface ClientAddController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) CedarButton *comfirmBtn;
@property (nonatomic,strong) UILabel *titleLblFirst;
@property (nonatomic,strong) UILabel *titleLblSecond;
@property (nonatomic,strong) UIImageView *arrowImgView;

@property (nonatomic,strong) NSString *identify;


@end

@implementation ClientAddController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"添加客户";
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.mas_equalTo(self.view);
    }];
    
    [self.tableView addSubview:self.comfirmBtn];
    [self.comfirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view.mas_bottom).mas_offset(-60);
        make.left.mas_equalTo(self.view).mas_offset(kMarginOffsetX);
        make.right.mas_equalTo(self.view).mas_offset(-kMarginOffsetX);
        make.height.mas_offset(40);
    }];

}

#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    //section头部高度
    return 40.00f;
}

//section头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
   
    if (section == 0) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = kAppBackgoundColor;
        [view addSubview:self.titleLblFirst];
        [self.titleLblFirst mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(view.mas_left).offset(15);
            make.centerY.equalTo(view.mas_centerY);
        }];
        return view;
    }else{
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = kAppBackgoundColor;
        [view addSubview:self.titleLblSecond];
        [self.titleLblSecond mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(view.mas_left).offset(15);
            make.centerY.equalTo(view.mas_centerY);
        }];
        return view;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    InputTempletTwelveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InputTempletTwelveTableViewCellIdentifier"];
    if (indexPath.section == 0) {
         if (indexPath.row == 0) {
               cell.placeholder = @" ";
               cell.cellTitle =  @"客户身份";
               cell.inputTextField.enabled =  NO;
               cell.cellImage = @"client_idetify";
             if (self.identify) {
                 cell.inputTextField.text = self.identify;
             }else {
                 cell.inputTextField.text = @"企业";
             }
             [self setupCell:cell.contentView];

           }else if (indexPath.row == 1) {
               cell.placeholder = @"请输入完整的公司名称";
               cell.cellTitle =  @"单位名称";
               cell.cellImage = @"client_company";
               cell.inputMaxLength = 20;
               cell.inputTextField.keyboardType =  UIKeyboardTypeDefault;
               @weakify(self)
               cell.textChangeBlock = ^(NSInteger tag, id body) {
                   @strongify(self)
                   
               };
           }else if (indexPath.row == 2) {
               cell.placeholder = @"请填写部门名称（可不填）";
               cell.cellTitle =  @"所属部门";
               cell.cellImage = @"client_department";

               cell.inputMaxLength = 20;
               cell.inputTextField.keyboardType =  UIKeyboardTypeDefault;
               @weakify(self)
               cell.textChangeBlock = ^(NSInteger tag, id body) {
                   @strongify(self)
                   
               };
           }else if (indexPath.row == 3) {
               cell.placeholder = @"请输入姓名";
               cell.cellTitle =  @"联系人";
               cell.cellImage = @"client_contact";
               cell.inputMaxLength = 20;
               cell.inputTextField.keyboardType =  UIKeyboardTypeDefault;
               @weakify(self)
               cell.textChangeBlock = ^(NSInteger tag, id body) {
                   @strongify(self)
                   
               };
           }else if (indexPath.row == 4) {
               cell.placeholder = @"请输入联系人号码";
               cell.cellTitle =  @"联系方式";
               cell.cellImage = @"client_phone";

               cell.inputMaxLength = 20;
               cell.inputTextField.keyboardType =  UIKeyboardTypeDefault;
               @weakify(self)
               cell.textChangeBlock = ^(NSInteger tag, id body) {
                   @strongify(self)
                   
               };
           }else if (indexPath.row == 5) {
               cell.placeholder = @"广州市天河区南村镇板桥村3组168号";
               cell.cellTitle =  @"公司地址";
               cell.cellImage = @"client_address";

               cell.inputMaxLength = 20;
               cell.inputTextField.keyboardType =  UIKeyboardTypeDefault;
               @weakify(self)
               cell.textChangeBlock = ^(NSInteger tag, id body) {
                   @strongify(self)
                   
               };
               return cell;

           }
    }else {
        if (indexPath.row == 0) {
                     cell.placeholder = @"请输入发票抬头";
                     cell.cellTitle =  @"发票抬头";
                     cell.cellImage = @"client_ticket";

                     cell.inputMaxLength = 20;
                     cell.inputTextField.keyboardType =  UIKeyboardTypeDefault;
                     @weakify(self)
                     cell.textChangeBlock = ^(NSInteger tag, id body) {
                         @strongify(self)
                         
                     };
                 }else if (indexPath.row == 1) {
                     cell.placeholder = @"请输入纳税人识别码";
                     cell.cellTitle =  @"纳税人识别码";
                     cell.cellImage = @"client_invoice_number";
                     cell.inputMaxLength = 20;
                     cell.inputTextField.keyboardType =  UIKeyboardTypeDefault;
                     cell.titleLenth = 120;
                     @weakify(self)
                     cell.textChangeBlock = ^(NSInteger tag, id body) {
                         @strongify(self)
                         
                     };
                 }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
           if (indexPath.row == 0) {
               [self selectcClientIdentify:indexPath];
           }
       }
}


 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
     
     return  [InputTempletTwelveTableViewCell heightForCell];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return   2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 6;
    }else {
        return 2;
    }
    return 0;
}

- (void)setupCell:(UIView *)contentView {
    [contentView addSubview:self.arrowImgView];
    [self.arrowImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(contentView).mas_offset(-kMarginOffsetX);
        make.centerY.mas_equalTo(contentView);
        make.width.mas_equalTo(8);
        make.height.mas_equalTo(14);
    }];

}

- (void)selectcClientIdentify:(NSIndexPath *)indexPath {
    
    NSMutableArray *actionSheetItems = [@[FSActionSheetTitleItemMake(FSActionSheetTypeNormal, @"企业"),
                                          FSActionSheetTitleItemMake(FSActionSheetTypeNormal, @"政府单位"),
                                          FSActionSheetTitleItemMake(FSActionSheetTypeNormal, @"旅行社"),FSActionSheetTitleItemMake(FSActionSheetTypeNormal, @"个人"),]
                                        mutableCopy];
    FSActionSheet *actionSheet = [[FSActionSheet alloc] initWithTitle:nil cancelTitle:nil items:actionSheetItems];
    // 展示并绑定选择回调
    [actionSheet showWithSelectedCompletion:^(NSInteger selectedIndex) {
        FSActionSheetItem *item = actionSheetItems[selectedIndex];
        self.identify = item.title;
        [self.tableView reloadData];
    }];
    
}



- (CedarButton *)comfirmBtn{
    if (!_comfirmBtn){
        _comfirmBtn =  [CedarButton buttonWithTextForGradientForCommomn:@"确认" radius:YES];
        @weakify(self)
        _comfirmBtn.clickEvent = ^{
            @strongify(self)
        };
    }
    return _comfirmBtn;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [UIConfigure tableViewFromFactory];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 250;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0)];
        [_tableView registerClass:[InputTempletTwelveTableViewCell class] forCellReuseIdentifier:@"InputTempletTwelveTableViewCellIdentifier"];
        [_tableView registerClass:[InputTempletTwelveTableViewCell class] forCellReuseIdentifier:@"InputTempletTwelveTableViewCellIdentifier"];

    }
    return _tableView;
}

MACRO_UILabelWithPFCGFont(titleLblFirst,@"基本信息",kGrayColor,15)
MACRO_UILabelWithPFCGFont(titleLblSecond,@"发票信息",kGrayColor,15)

MACRO_UIImageView(arrowImgView, @"commen_arrow")

@end

